package com.jxinternet.platform.services.coder;

import com.google.protobuf.ByteString;
import com.google.protobuf.MessageLite;
import com.jxinternet.platform.services.entity.protobuf.BattleProtos.BattleResponse;
import com.jxinternet.platform.services.entity.protobuf.LoginProtos.LoginResponse;
import com.jxinternet.platform.services.entity.protobuf.LogoutProtos.LogoutResponse;
import com.jxinternet.platform.services.entity.protobuf.RegisterProtos.RegisterResponse;
import com.jxinternet.platform.services.entity.protobuf.ServerTimeProtos.ServerTimeResponse;
import com.jxinternet.platform.services.entity.protobuf.WealthProtos.WealthResponse;
import com.jxinternet.platform.services.protocol.BaseProtocol;

/**
 * 编码器2
 * 编最外层第二层
 * Created by gold-jx on 2017/6/17.
 */
public class MessageEncoder {

    public ByteString toEncode(MessageLite message, int area) throws Exception {
        byte[] body;
        ByteString byteString;
        switch (area) {
            case BaseProtocol.Area_Login:
                LoginResponse loginResponse = (LoginResponse) message;
                body = loginResponse.toByteArray();
                byteString = ByteString.copyFrom(body);
                return byteString;
            case BaseProtocol.Area_User_Register:
                RegisterResponse registerResponse = (RegisterResponse) message;
                body = registerResponse.toByteArray();
                byteString = ByteString.copyFrom(body);
                return byteString;
            case BaseProtocol.Area_User_LoadWealth:
                WealthResponse wealthResponse = (WealthResponse) message;
                body = wealthResponse.toByteArray();
                byteString = ByteString.copyFrom(body);
                return byteString;
            case BaseProtocol.Area_Battle:
                BattleResponse battleResponse = (BattleResponse) message;
                body = battleResponse.toByteArray();
                byteString = ByteString.copyFrom(body);
                return byteString;
            case BaseProtocol.Area_Wizard_GetServerTime:
                ServerTimeResponse serverTimeResponse= (ServerTimeResponse) message;
                body = serverTimeResponse.toByteArray();
                byteString = ByteString.copyFrom(body);
                return byteString;
            case BaseProtocol.Area_Logout:
                LogoutResponse logoutResponse= (LogoutResponse) message;
                body = logoutResponse.toByteArray();
                byteString = ByteString.copyFrom(body);
                return byteString;
            default:
                break;
        }
        return null;
    }


    public static void main(String[] args) throws Exception {
        LoginResponse loginResponse = LoginResponse.newBuilder()
                .setToken("123456")
                .setUserName("zhangsan")
                .build();

        MessageEncoder messageEncoder = new MessageEncoder();

        ByteString byteString = messageEncoder.toEncode(loginResponse, 1002);

    }
}
